{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 20. Valid Parentheses 有效的括号\n",
    "\n",
    "### 难度：Easy\n",
    "\n",
    "## 刷题内容\n",
    "\n",
    "> 原题链接\n",
    "\n",
    " - 中文：https://leetcode-cn.com/problems/valid-parentheses/description\n",
    " - 英文：https://leetcode.com/problems/valid-parentheses\n",
    " \n",
    "> 内容描述\n",
    "\n",
    "```\n",
    "给定一个只包括 '('，')'，'{'，'}'，'['，']' 的字符串，判断字符串是否有效。\n",
    "\n",
    "有效字符串需满足：\n",
    "\n",
    "1、左括号必须用相同类型的右括号闭合。\n",
    "2、左括号必须以正确的顺序闭合。\n",
    "\n",
    "注意空字符串可被认为是有效字符串。\n",
    "\n",
    "示例1：\n",
    "输入: \"()\"\n",
    "输出: true\n",
    "\n",
    "示例2：\n",
    "输入: \"()[]{}\"\n",
    "输出: true\n",
    "\n",
    "示例3：\n",
    "输入: \"(]\"\n",
    "输出: false\n",
    "\n",
    "示例4：\n",
    "输入: \"([)]\"\n",
    "输出: false\n",
    "\n",
    "示例5：\n",
    "输入: \"{[]}\"\n",
    "输出: true\n",
    "```\n",
    "\n",
    "## 解题方案\n",
    "\n",
    "> 思路 1\n",
    "\n",
    "我们只需要匹配三种情况： \"(\" -> \")\", \"[\" -> \"]\", \"{\" -> \"}\".\n",
    "\n",
    "但是这里最重要的思想是 栈 。一遇到左括号就入栈，右括号出栈，这样来寻找对应。\n",
    "\n",
    "需要检查几件事：\n",
    "\n",
    " - 出现右括号时 stack 里是否还有符号（无论左右）\n",
    " - 出 stack 时是否对应\n",
    " - 最终 stack 是否为空"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "class Solution:\n",
    "    def isValid(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        LEFT = {'(', '[', '{'}  # 左括号\n",
    "        RIGHT = {')', ']', '}'}  # 右括号\n",
    "        stack = []  # 创建一个栈\n",
    "        for brackets in s:  # 迭代传过来的所有字符串\n",
    "            if brackets in LEFT:  # 如果当前字符在左括号内\n",
    "                stack.append(brackets)  # 把当前左括号入栈\n",
    "            elif brackets in RIGHT:  # 如果是右括号\n",
    "                if not stack or not 1 <= ord(brackets) - ord(stack[-1]) <= 2:\n",
    "                    # 如果当前栈为空，()]\n",
    "                    # 如果右括号减去左括号的值不是小于等于2大于等于1\n",
    "                    return False  # 返回False\n",
    "                stack.pop()  # 删除左括号\n",
    "        return not stack  # 如果栈内没有值则返回True，否则返回False\n",
    "    \n",
    "s = Solution()\n",
    "print(s.isValid(\"([[])[]{}\"))\n",
    "print(s.isValid(\"([])[]{}\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与 思路 1 相同，但是更加容易理解的版本："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "class Solution(object):\n",
    "    def isValid(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        leftP = '([{'\n",
    "        rightP = ')]}'\n",
    "        stack = []\n",
    "        for char in s:\n",
    "            if char in leftP:\n",
    "                stack.append(char)\n",
    "            if char in rightP:\n",
    "                if not stack:\n",
    "                    return False\n",
    "                tmp = stack.pop()\n",
    "                if char == ')' and tmp != '(':\n",
    "                    return False\n",
    "                if char == ']' and tmp != '[':\n",
    "                    return False       \n",
    "                if char == '}' and tmp != '{':\n",
    "                    return False\n",
    "        return stack == []\n",
    "    \n",
    "s = Solution()\n",
    "print(s.isValid(\"([[])[]{}\"))\n",
    "print(s.isValid(\"([])[]{}\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 思路 2\n",
    "\n",
    " - 扩展性和可理解性更强\n",
    " - 使用字典类型来存储对应关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "class Solution:\n",
    "    def isValid(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if len(s) % 2 == 1:\n",
    "            return False\n",
    "\n",
    "        index = 0\n",
    "        stack = [i for i in s]\n",
    "        map1 = {\"(\": \")\", \"[\": \"]\", \"{\": \"}\"}\n",
    "\n",
    "        while len(stack) > 0:\n",
    "            # 判断索引是否超过边界\n",
    "            if index >= len(stack)-1:\n",
    "                return False\n",
    "    \n",
    "            b = stack[index]\n",
    "            e = stack[index+1]\n",
    "\n",
    "            if b not in map1.keys():\n",
    "                return False\n",
    "            elif e in map1.keys():\n",
    "                index += 1\n",
    "            elif map1[b] == e:\n",
    "                stack.pop(index+1)\n",
    "                stack.pop(index)\n",
    "                index = 0 if index-1<0 else index-1\n",
    "            else:\n",
    "                return False\n",
    "\n",
    "        return stack == []\n",
    "    \n",
    "s = Solution()\n",
    "print(s.isValid(\"([[])[]{}\"))\n",
    "print(s.isValid(\"([])[]{}\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 思路 3\n",
    "\n",
    " - 括号匹配，我们可以换用另一种方式\n",
    " - 首先，不管它是否符合第一个符号是左括号的要求，我们先把它放到list 中，作为栈，然后一个一个遍历，符合配对顺序就弹出，最终只需要判断是否栈为空就可以了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "l_d = {\n",
    "    '{': -3,\n",
    "    '(': -2,\n",
    "    '[': -1,\n",
    "    ']': 1,\n",
    "    ')': 2,\n",
    "    '}': 3,\n",
    "}\n",
    "    \n",
    "class Solution:\n",
    "    def isValid(self, s):\n",
    "        l_s = []\n",
    "        for c_r in s:\n",
    "            if len(l_s) == 0:\n",
    "                l_s.append(c_r)\n",
    "                continue\n",
    "\n",
    "            c_l = l_s[-1]\n",
    "\n",
    "            if l_d[c_l] + l_d[c_r] == 0 and l_d[c_l] < 0:\n",
    "                l_s.pop()\n",
    "            else:\n",
    "                l_s.append(c_r)\n",
    "\n",
    "        if len(l_s) == 0:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        \n",
    "s = Solution()\n",
    "print(s.isValid(\"([[])[]{}\"))\n",
    "print(s.isValid(\"([])[]{}\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
